package com.wonders.hs.pd.mm.web.controller;

import com.wonders.hs.pd.mm.web.utils.JDBCConnect;
import com.wonders.hs.pd.mm.web.utils.MedicareCodeExcelUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static com.wonders.hs.pd.mm.web.controller.ExcelController.EXCEL_TYPE_COMMON;
import static com.wonders.hs.pd.mm.web.utils.ExcelFileGenerator.buildExcelDocument;

/**
 * @author banglong
 * @date 2017/5/3
 * 部门独立的报表导出功能
 */
@Controller
@RequestMapping("/deptExcelExport")
public class DeptReportExcel {

  /**
   * 供货商不良记录导出
   */
  @RequestMapping(value = "/badRecord")
  public void badRecord(String startTime,
                        String endTime,
                        HttpServletResponse response) {
    List<Map<String, String>> mapList = JDBCConnect.badRecordQuery(startTime, endTime);
    exportExcel(startTime + "~" + endTime + "供货商不良记录报表", EXCEL_TYPE_COMMON, mapList, response);
  }

  /**
   * 供货商不良记录导出
   */
  @RequestMapping(value = "/medicareCode")
  public void medicareCode(String startTime,
                           String endTime,
                           int count,
                           HttpServletResponse response) {
    List<Map<String, String>> mapList = JDBCConnect.medicareCodeQuery(startTime, endTime, count);

    // 结果数据
    List<Map<String, String>> result = new ArrayList<>();
    String fieldName = "";

    // 循环处理结果
    if (!"暂无数据".equals(mapList.get(0).get("查询结果"))) {
      String supplierName = ""; // 供应商名称
      int index = 1;

      for (int i = 0; i < mapList.size(); i++, index++) {
        // 获取结果
        LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) mapList.get(i);
        // 第一次时候需要加入
        if (i == 0) {
          result.add(newTitle(map));
        }
        // 判断供应商是否为空
        if (StringUtils.isEmpty(supplierName)) {
          supplierName = map.get("供应商名称");
        }
        // 判断供应商是否是上次的供应商 如果不是 重新开始 加入 title
        if (!supplierName.equals(map.get("供应商名称"))) {
          index = 1;
          supplierName = map.get("供应商名称");
          result.add(newTitle(map));
        }
        // 加入序号
        map.put("序号", String.valueOf(index));
        result.add(map);
      }

      StringBuilder builder = new StringBuilder();
      // 序号需要放在第一位
      if (mapList.size() > 0) {
        builder.append("序号,");
        Map<String, String> map = mapList.get(0);
        for (Map.Entry<String, String> entry : map.entrySet()) {
          if (!"序号".equals(entry.getKey()) &&
                  !"供应商名称".equals(entry.getKey()) &&
                  !"供应商联系人".equals(entry.getKey()) &&
                  !"供应商联系电话".equals(entry.getKey()) &&
                  !"记录日期".equals(entry.getKey())) {
            builder.append(entry.getKey());
            builder.append(",");
          }
        }
      }

      fieldName = builder.toString().substring(0, builder.toString().length() - 1);
    }

    Map<String, Object> page_div = new HashMap<>();

    page_div.put("name", startTime + "~" + endTime + "医保编码审核表");
    page_div.put("data", result);
    page_div.put("fieldName", fieldName);

    MedicareCodeExcelUtil.exportExcelFile(page_div, response);
  }

  /**
   * @description 导出excel文件
   */
  private void exportExcel(String name, int type, List<Map<String, String>> mapList, HttpServletResponse response) {
    Map<String, Object> page_div = new HashMap<>();
    StringBuilder builder = new StringBuilder();
    if (mapList.size() > 0) {
      Map<String, String> map = mapList.get(0);
      for (Map.Entry<String, String> entry : map.entrySet()) {
        builder.append(entry.getKey());
        builder.append(",");
      }
    }
    page_div.put("name", name);
    page_div.put("data", mapList);
    String filedName = "";
    // 需要清除 第一个
    if (builder.length() > 0) {
      filedName = builder.toString().substring(0, builder.toString().length() - 1);
    }

    buildExcelDocument(page_div, response, filedName, filedName, type);
  }

  private Map<String, String> newTitle(Map<String, String> map) {
    Map<String, String> titleMap = new HashMap<>();
    // title 标识
    titleMap.put("title", "title");
    titleMap.put("供应商名称", map.get("供应商名称"));
    titleMap.put("供应商联系人", map.get("供应商联系人"));
    titleMap.put("供应商联系电话", map.get("供应商联系电话"));
    return titleMap;
  }
}
